package org.dt.common.sequence;

import org.dt.common.sequence.persist.CasSeqMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yujiadong
 * @create: 2018/12/03 10:33
 **/
@Slf4j
public class CasDelegator extends BaseDelegator {
    private CasSeqMapper mapper;

    public CasDelegator(CasSeqMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public Sequence applyForOneStep(String biz, long step) {
        Sequence sequence = null;
        long     maxId;
        while (sequence == null) {
            maxId = mapper.queryMaxId(biz);
            if (mapper.updateMaxId(biz, maxId, maxId + step) > 0) {
                sequence = new Sequence(maxId + 1, maxId + step);
            }
        }
        return sequence;
    }

}
